<?php

class BanTinController extends Zend_Controller_Action {
	
	public function init() {
		/* Initialize action controller here */
	}
	
	public function indexAction() {
		// action body
	}
	
	public function addBanTinAction() {
	
	}
	
	public function addSuccessBanTinAction() {
		$hinhKemBanTin = array ();
		
		$tieuDe = $this->_request->getParam ( 'tieuDe' );
		$tab = $this->_request->getParam ( 'tab' );
		$noiDung = $this->_request->getParam ( 'noiDung' );
		$hinhKemBanTin = Application_Model_util_MyImageUtil::makeUpload ( 'hinhKemBanTin', $this->getRequest (), 600, 600 );
		$maDanhMucBanTin = $this->_request->getParam ( 'maDanhMucBanTin' );
		$maTaiKhoan = $this->_request->getParam ( 'maTaiKhoan' );
		
		$service = Application_Model_service_BanTinService::getInstance ();
		$service instanceof Application_Model_service_BanTinService;
		$result = $service->addBanTin ( $tieuDe, $tab, $noiDung, $hinhKemBanTin, $maDanhMucBanTin, $maTaiKhoan );
	}
	
	public function editBanTinAction() {
		$id = $this->_request->getParam ( 'maBanTin' );
		$banTins = Application_Model_service_BanTinService::getInstance ();
		$banTin = $banTins->getBanTin ( $id );
		
		$this->view->maBanTin = $banTin->getMaBanTin ();
		$this->view->tieuDe = $banTin->getTieuDe ();
		$this->view->tab = $banTin->getTab ();
		$this->view->noiDung = $banTin->getNoiDung ();
		$this->view->hinhKemBanTin = $banTin->getHinhKemBanTin ();
		$this->view->maDanhMucBanTin = $banTin->getMaDanhMucBanTin ();
		$this->view->maTaiKhoan = $banTin->getMaTaiKhoan ();
	}
	
	public function editSuccessBanTinAction() {
		$maBanTin = $this->_request->getParam ( 'maBanTin' );
		$tieuDe = $this->_request->getParam ( 'tieuDe' );
		$tab = $this->_request->getParam ( 'tab' );
		$noiDung = $this->_request->getParam ( 'noiDung' );
		$hinhKemBanTin = $this->_request->getParam ( 'hinhKemBanTin' );
		$maDanhMucBanTin = $this->_request->getParam ( 'maDanhMucBanTin' );
		$maTaiKhoan = $this->_request->getParam ( 'maTaiKhoan' );
		
		$service = Application_Model_service_BanTinService::getInstance ();
		$result = $service->editBanTin ( $array );
		
		$this->view->result = $result;
	}
	
	public function deleteBanTinAction() {
		// action body
		

		$id = $this->_request->getParam ( 'maBanTin' );
		$banTins = Application_Model_service_BanTinService::getInstance ();
		$banTin = $banTins->deleteBanTin ( $id );
		
		$this->view->result = $banTin;
	}
	
	public function getTopBanTinAction() {
		$service = Application_Model_service_BanTinService::getInstance ();
		$result = $service->getTopBanTin ();
		
		//add view
		$this->view->list = $result;
	}
	
	public function getBanTinTheoDanhMucAction() {
		$maDanhMuc = $this->_request->getParam ( 'maDanhMuc' );
		
		$service = Application_Model_service_BanTinService::getInstance ();
		$result = $service->getBanTinTheoDanhMuc ( $maDanhMuc );
		
		//add view
		$this->view->list = $result;
	}
	
	public function getAllBanTinAction() {
		$service = Application_Model_service_BanTinService::getInstance ();
		$result = $service->getAllBanTin ();
		
		//add view
		$this->view->list = $result;
	}
	
	public function newAction() {
		$service = Application_Model_service_DanhMucBanTinService::getInstance ();
		$service instanceof Application_Model_service_DanhMucBanTinService;
		
		$danhMuc = $service->getAllDanhMucBanTin ();
		$this->view->danhMuc = $danhMuc;
	}
	
	public function successAction() {
		$hinhKemBanTin = array ();
		
		$tieuDe = $this->_request->getParam ( 'tieuDe' );
		$tab = $this->_request->getParam ( 'tab' );
		$noiDung = $this->_request->getParam ( 'noiDung' );
		$hinhKemBanTin = Application_Model_util_MyImageUtil::makeUpload ( 'bantin', $this->getRequest (), 600, 600 );
		$maDanhMucBanTin = $this->_request->getParam ( 'maDanhMucBanTin' );
		
		$service = Application_Model_service_BanTinService::getInstance ();
		$service instanceof Application_Model_service_BanTinService;
		$result = $service->addBanTin ( $tieuDe, $tab, $noiDung, $hinhKemBanTin [0], $maDanhMucBanTin, $maTaiKhoan );
	}
	
	public function updateAction() {
		$maBanTin = $_POST ['updateBanTin'];
		$banTinService = Application_Model_service_BanTinService::getInstance ();
		$banTinService instanceof Application_Model_service_BanTinService;
		$banTin = $banTinService->getBanTin ( $maBanTin );
		$this->view->banTin = $banTin;
		
		$service = Application_Model_service_DanhMucBanTinService::getInstance ();
		$service instanceof Application_Model_service_DanhMucBanTinService;
		$danhMuc = $service->getAllDanhMucBanTin ();
		$this->view->danhMuc = $danhMuc;
	}
	
	public function deleteAction() {
		$id = $_POST ['deleteBanTin'];
		$banTins = Application_Model_service_BanTinService::getInstance ();
		$banTin = $banTins->deleteBanTin ( $id );
		
		$this->view->result = $banTin;
	}
	
	public function updatesuccessAction() {
		$hinhKemBanTin = array ();
		$maTaiKhoan = $_SESSION ['id'] = 2;
		$maBanTin = $this->_request->getParam ( 'maBanTin' );
		$tieuDe = $this->_request->getParam ( 'tieuDe' );
		$tab = $this->_request->getParam ( 'tab' );
		$noiDung = $this->_request->getParam ( 'noiDung' );
		try {
			$hinhKemBanTin = Application_Model_util_MyImageUtil::makeUpload ( 'bantin', $this->getRequest (), 600, 600 );
		} catch ( Zend_5250_Exception $e ) {
			echo 'hinh cu';
		}
		$maDanhMucBanTin = $this->_request->getParam ( 'maDanhMucBanTin' );
		echo 'madanhmuc ' . $maDanhMucBanTin;
		$service = Application_Model_service_BanTinService::getInstance ();
		$service instanceof Application_Model_service_BanTinService;
		$result = $service->editBanTin ( $maBanTin, $tieuDe, $tab, $noiDung, $hinhKemBanTin [0], $maDanhMucBanTin, $maTaiKhoan );
		echo 'result ' . $result;
	}

}

